package cn.e3mall.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.mapper.TbItemCatMapper;
import cn.e3mall.pojo.TbItemCat;
import cn.e3mall.pojo.TbItemCatExample;
import cn.e3mall.pojo.TbItemCatExample.Criteria;
import cn.e3mall.service.ItemCatService;
@Service
@Transactional
public class ItemCatServiceImlpl implements ItemCatService {
	@Autowired
	private TbItemCatMapper itemCatMapper;
	
	@Override
	public List<EasyUITreeNode> getTreeList(long parentedId) {
	
		//创建集合用于返回
		List<EasyUITreeNode> treeNodeList = new ArrayList<>();
		//
		TbItemCatExample example = new TbItemCatExample();
		//
		Criteria criteria = example.createCriteria();
		//添加查询条件
		criteria.andParentIdEqualTo(parentedId);
		//返回查询结果
		List<TbItemCat> list = itemCatMapper.selectByExample(example);
	
		for (TbItemCat tbItemCat : list) {
			//创建返回的json对象EasyUITreeNode
			EasyUITreeNode treeNode = new EasyUITreeNode();
			/**
			 *  "id": 1,    
			    "text": "Node 1",    
			    "state": "closed"
			 */
			//添加treeNode的ID
			treeNode.setId(tbItemCat.getId());
			//添加内容
			treeNode.setText(tbItemCat.getName());
			//是否是父类ID
			treeNode.setState(tbItemCat.getIsParent()?"closed":"open");
			
			treeNodeList.add(treeNode);
		}
		
		return treeNodeList;
	}

}
